<?php

namespace app\index\controller;

use app\data\logic\CertificationsLogic;
use app\data\logic\PageLogic;
use app\data\logic\PartnerLogic;
use app\data\logic\RecruitmentLogic;
use app\data\logic\VideosLogic;
use app\index\BaseController;
use http\Client;
use PHPMailer\PHPMailer\PHPMailer;
use think\App;
use think\Db;
use think\Exception;
use think\facade\Log;
use think\facade\View;

class Page extends BaseController
{

    protected CertificationsLogic $certificationsLogic;
    protected PartnerLogic $partnerLogic;
    protected RecruitmentLogic $recruitmentLogic;
    protected VideosLogic $videosLogic;

    /**
     * 当前页码
     * @var int
     */
    protected int $pageIndex;

    /**
     * 显示数量
     * @var int
     */
    protected int $pageSize;

    /**
     * 分页数据
     * @var array
     */
    protected array $pageInfo;

    /**
     * 数据列表
     * @var array
     */
    protected array $dataList;

    /**
     * 单个数据集
     * @var array
     */
    protected array $dataItem;

    /**
     * 分类列表
     * @var array
     */
    protected array $catList;

    /**
     * 分类id
     * @var int
     */
    protected int $catId;

    /**
     * 数据id
     * @var int
     */
    protected int $dataId;

    /**
     * 跳转的路由
     * @var string
     */
    protected string $redirectName;

    /**
     * 是否引入表单
     * @var bool
     */
    protected bool $needForms;

    /**
     * 分类信息
     * @var array
     */
    protected array $catInfo;


    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->certificationsLogic = new CertificationsLogic();
        $this->partnerLogic = new PartnerLogic();
        $this->recruitmentLogic = new RecruitmentLogic();
        $this->videosLogic = new VideosLogic();
        $this->dataList = [];
        $this->pageInfo = [];
        $this->dataItem = [];
        $this->catList = [];
        $this->catId = 0;
        $this->dataId = 0;
        $this->redirectName = 'common';
        $this->needForms = false;
        $this->catInfo = [];
    }


    public function __call(string $name, array $arguments)
    {
        $this->pageIndex = $this->request->param('page', 1);
        $this->pageSize = $this->request->param('limit', 10);
        switch ($name) {
            case 'index':
                $this->doIndex();
                break;
            case 'bloginfo':
                $this->doBloginfo();
                break;
            case 'partner':
                $this->doPartner();
                break;
            case 'partnerinfo':
                $this->doPartnerInfo();
                break;
            case 'certifications':
                $this->doCertifications();
                break;
            case 'services':
                $this->doServices();
                break;
            case 'blog':
                $this->doBlog();
                break;
            case 'joinus':
                $this->doJoinus();
                break;
            case 'casesinfo':
                $this->doCasesInfo();
                break;
            case 'cases':
                $this->doCases();
                break;
            case 'service':
                $this->doService();
                break;
            case 'download':
                $this->doDownload();
                break;
            case 'faq':
                $this->doFaq();
                break;
            case 'about':
                $this->doAbout();
                break;
            case 'product':
                $this->doProduct();
                break;
            case 'contact':
                $this->doContact();
                break;
            case 'productDetail':
                $this->doProductDetail();
                break;
            case 'search':
                $this->doSearch();
                break;
        }
        View::assign('pageIndex', $this->pageIndex);
        View::assign('pageInfo', $this->pageInfo);
        View::assign('dataList', $this->dataList);
        View::assign('dataItem', $this->dataItem);
        View::assign('catList', $this->catList);
        View::assign('catId', $this->catId);
        View::assign('catInfo', $this->catInfo);
        View::assign('dataId', $this->dataId);
        View::assign('needForms', $this->needForms);
        return $this->responseFetchHtml($this->redirectName);
    }

    private function doProductCatAll()
    {
        /*  $childs=$this->page->getProductsCategoryList(['all'=>1,'parent_id'=>0]);
          View::assign('childs',$childs['list']);
          foreach ($this->menus as &$value){
              if($value['en_name']=='product') {
                  $value['active']=true;
              }
          }
          View::assign('menuList', $this->menus);*/
        $centerProducts1 = $this->page->getProductsList(['cate_id', 7]);
        View::assign('centerProducts1', $centerProducts1['list']);
        $centerProducts2 = $this->page->getProductsList(['cate_id', 8]);
        View::assign('centerProducts2', $centerProducts2['list']);
        $centerProducts3 = $this->page->getProductsList(['cate_id', 7]);
        View::assign('centerProducts3', $centerProducts3['list']);
        $centerProducts4 = $this->page->getProductsList(['cate_id', 8]);
        View::assign('centerProducts4', $centerProducts4['list']);
        $this->redirectName = 'product-cat-all';
    }

    private function doProductCatOne()
    {
        // 获取page_id
        $cate_id = $this->request->param('id', $this->proCat['list'][0]['id']);
        if ($this->request->param('catId')) {
            $cate_id = $this->request->param('catId');
        }
        $this->catId = $cate_id;

        // 分类信息
        $catInfo = $this->page->getProductCategoryDetail($cate_id)['data'];
        $this->catInfo = $catInfo;

        $showFields = $catInfo['relationFields'];
        $showFieldsIds = [];
        for ($i = 0; $i < count($showFields); $i++) {
            $showFieldsIds[] = $showFields[$i]['id'];
        }
        // 页面显示 产品分类应有的字段
        View::assign('showFieldsIds', $showFieldsIds);
        $pageIndex = $this->request->param('page', 1);
        $productParams['page'] = $pageIndex;

        if ($this->request->param('keywords')) {
            $productParams['keywords'] = $this->request->param('keywords');
        }

        if ($this->request->param('id')) {
            $productParams['cate_id'] = $this->request->param('id');
        }
        View::assign('catInfo', $this->catInfo);
        // 产品列表分页
        $productList = $this->page->getProductsList($productParams);
        $this->dataList = $productList['list'];
        $this->pageIndex = $pageIndex;
        $this->pageInfo = $productList['pageInfo'];
        View::assign('keywords', $this->request->param('keywords', ''));
        $pageHtml = $catInfo['inner_content'];
        View::assign('pageHtml', $pageHtml);
        $childs = $this->page->getProductsCategoryList(['all' => 1, 'parent_id' => $cate_id]);
        View::assign('childs', $childs['list']);
        foreach ($this->menus as &$value) {
            if ($value['en_name'] == 'product') {
                $value['active'] = true;
            }
        }
        View::assign('menuList', $this->menus);
        $this->redirectName = 'product-cat';
    }

    public function doSearch()
    {
        $keywords = $this->request->param('keywords') ?? "";
        View::assign('showKeywords', $keywords);
        $allList = [];
        if ($keywords != '') {
            $keywords = str_replace('；', ';', $keywords);
            $keywords = explode(';', $keywords);
            $search['keywords'] = $keywords;
            $cmsList = $this->page->getCmsList(['all' => 1, 'keywords' => $keywords]);
            $casesList = $this->page->getCasesSearchList(['is_show' => 1], $search);
            foreach ($cmsList as &$value) {
                $row['title'] = $value['title'];
                $row['from'] = '新闻资讯';
                $row['url'] = "/bloginfo?id={$value['article_id']}";
                $row['dec'] = $value['content2'];
                $row['cover'] = $value['photo'];
                $allList[] = $row;
            }
            foreach ($casesList as &$value) {
                $row['title'] = $value['name'];
                $row['from'] = '案例';
                $row['url'] = "/casesinfo?id={$value['id']}";
                $row['dec'] = $value['desc'];
                $row['cover'] = $value['pic'];
                $allList[] = $row;
            }
        }
        if (!empty($keywords)) {
            View::assign('searchKeywords', $keywords);
        } else {
            View::assign('searchKeywords', []);
        }
        View::assign('count', count($allList));
        View::assign('allList', $allList ?? []);
        View::assign('cmsList', $cmsList ?? []);
        View::assign('keywords', $keywords ? implode(';', $keywords) : "");
        $this->redirectName = 'search';
    }

    public function doCases()
    {
        $this->pageSize = 6;
        $params = [
            'page' => $this->pageIndex,
            'limit' => 9,
        ];
        $result = $this->page->getCaseList($params);
        $this->dataList = $result['list'];
        $this->pageInfo = $result['pageInfo'];

        $this->redirectName = 'cases';

    }

    public function getCaseForCat($catId)
    {
        $catInfo = $this->page->getCaseCategoryDetail($catId);
        $this->catInfo = $catInfo['data'];
    }

    private function getCases($count = 10, $search = null)
    {
        if ($this->catId == 5) {
            $cases = $this->page->getCasesListByPages(
                $this->pageIndex,
                $count,
                0,
                ['is_show' => 1, 'cat_ids' => $this->casesVideoChild],
                $search
            );
        } else {
            $cases = $this->page->getCasesListByPages(
                $this->pageIndex,
                $count,
                $this->catId,
                ['is_show' => 1],
                $search
            );
        }
        View::assign('cat_id', $this->catId);
        View::assign('cases', $cases['list']);
        View::assign('search', $search);
        $this->pageInfo = $cases['pageInfo'];
        $this->dataList = $cases['list'];
    }

    public function doCasesInfo()
    {
        $param = $this->request->param();
        $case = $this->page->getCaseDetail($param['id']);
        $rightCases = $this->page->getCaseForPreAndNextByCat($param['id'], $case['data']['cat_id']);
        // 获取相关
        $this->dataItem = $case['data'];
        View::assign('rightCases', $rightCases);
        $cat = $this->page->getCaseCategoryDetail($case['data']['cat_id']);
        $this->catInfo = $cat['data'];
        $this->catId = $case['data']['cat_id'];
        $this->redirectName = 'casesinfo';
    }

    /**
     * 新闻列表页面
     * @return string
     */
    private function doBlog()
    {
        $this->pageSize = 9;
        $result = $this->page->getArticleListByPages(
            $this->pageIndex,
            $this->pageSize
        );
        $this->pageInfo = $result['pageInfo'];
        $this->dataList = $result['list'];
        $this->redirectName = 'blog';
    }

    /**
     * 新闻详情页
     * @return string
     */
    public function doBloginfo()
    {
        $param = $this->request->param();

        $news = $this->page->getCmsDetail($param['id']);
        $rightNews = $this->page->getArticleForPreAndNext($param['id']);
        // 获取推荐文章
        $recommendData = $this->page->getArticleForRecommend();
        View::assign('recommendData', $recommendData);// 推荐文章列表
        View::assign('bloginfo', $news['data']);
        View::assign('rightNews', $rightNews);

        $this->catInfo = $this->page->getCmsCatDetail($news['data']['cmsCat']['cat_id'])['data'];
        $this->redirectName = 'bloginfo';
    }

    /**
     * 新闻详情页
     * @return string
     */
    public function doContact()
    {

        $this->redirectName = 'contact';
    }

    /**
     * advantage列表页面
     * @return string
     */
    private function doAdvantage()
    {
        $search = [
            'keywords' => "",
            'year' => "",
            'tag' => '',
        ];
        if ($this->request->isPost()) {
            $search = $this->request->post();
        }
        $search['tag'] = $this->request->param('tag');
        // 获取文章分类列表
        $this->getAdvantageCat();
        $this->catId = $this->request->param('id') ?? $this->catList[0]['cat_id'] ?? 0; // 分类id
        $this->pageSize = 9;
        // 文章列表
        $this->getBlogs($this->pageSize, $search);
        //$this->dataList = $this->page->getCmsList(['all' => 1, 'cat_id' => $this->catId]);
        // 文章分类
        $this->getBlogsCatDetail($this->catId);
//
//        // 获取推荐文章
        $recommendData = $this->page->getArticleForRecommend();
//        // 分页信息
        View::assign('recommendData', $recommendData);// 推荐文章列表
        $tags = $this->page->getTagsLib();
        View::assign('tags', $tags);
        View::assign('cat_id', $this->catId);
        if (isset($this->articePageId[$this->catId])) {
            $pagePartList = $this->page->getPagePartList($this->articePageId[$this->catId]);
            View::assign('pagePartList', $pagePartList);
        }
        if (isset($this->articePage[$this->catId])) {
            $this->redirectName = $this->articePage[$this->catId];
        } else {
            $this->redirectName = 'advantage';
        }
    }

    /**
     * 诚邀合作页面
     * @return string|\think\response\Json|void
     */
    public function invite()
    {
        if ($this->request->isGet()) { // 页面访问请求
            return $this->responseFetchHtml();
        } else if ($this->request->isPost()) { // 页面表单提交请求
            $params = $this->request->post();
            $params['ip'] = $this->request->ip();
            $params['url'] = $this->request->url();
            $params['msg_type'] = 1;
            $params['is_show'] = 1;
            $result = $this->page->createNewFeedBack($params);
            if ($result['state']) {
                return $this->respMsg(\config('code.SUCCESS'), \lang('SUCCESS'));
            } else {
                return $this->respMsg(\config('code.FAILURE'), \lang('FAILURE'));
            }
        }

    }


    /**
     * 服务支持
     */
    public function support()
    {
        if ($this->request->isGet()) { // 页面访问请求
            return $this->responseFetchHtml();
        } else if ($this->request->isPost()) { // 页面表单提交请求
            $params = $this->request->post();
            $params['ip'] = $this->request->ip();
            $params['url'] = $this->request->url();
            $params['msg_type'] = 2;
            $params['is_show'] = 1;
            $result = $this->page->createNewFeedBack($params);
            if ($result['state']) {
                return $this->respMsg(\config('code.SUCCESS'), \lang('SUCCESS'));
            } else {
                return $this->respMsg(\config('code.FAILURE'), \lang('FAILURE'));
            }
        }
    }

    /**
     * 首页
     * @return string
     */
    public function doIndex()
    {
//        $products = $this->page->getProductsList(['all' => 1]);
//        View::assign('products', $products);

        // 读取最新三条新闻
        $this->getBlogs(3);

//        案例
//        $industries = $this->page->getCaseList(['limit' => 3])['list'];
//        View::assign('industries', $industries);

        // 证书
//        $this->getCertifications(null);

        $this->redirectName = 'index';
    }

    /**
     * 产品中心
     * @return string
     */
    private function doProduct()
    {
        // 获取page_id
        $cate_id = $this->request->param('id');
        if ($this->request->param('catId')) {
            $cate_id = $this->request->param('catId');
        }
        $this->catId = $cate_id ?? 0;
        if ($this->catId) {
            // 分类信息
            $catInfo = $this->page->getProductCategoryDetail($cate_id)['data'];
            $this->catInfo = $catInfo;
        }

        $productParams = [
            'page' => $this->pageIndex,
            'limit' => 9,
            'is_show' => 1
        ];
        $result = $this->page->getProductsList($productParams);
        $this->dataList = $result['list'];
        $this->pageInfo = $result['pageInfo'];

        if ($this->catId) {
            $page_id = $this->catInfo['relationPage']['page_id'];
            $pagePartList = $this->page->getPagePartList($page_id);
            View::assign('pagePartList', $pagePartList);
            $this->redirectName = 'product-cat';
        } else {
            $this->redirectName = 'product';
        }
    }


    /**
     * 产品详情页面
     * @param $id
     * @return string|null
     */
    public function doProductDetail()
    {
        $id = $this->request->param('id');
        // 查询产品详情
        $result = $this->page->getProductDetail($id);
        $catInfo = $this->page->getProductCategoryDetail($result['cate_id'])['data'];
        $this->catInfo = $catInfo;
        $this->dataItem = $result;

        $this->getBlogs(5);
        $this->redirectName = 'product-detail';

    }

    /**
     * 反馈的action
     */
    public function feedback()
    {
        try {
            if ($this->request->isPost()) { //提交反馈
                $postData = $this->request->post();
                $postData['ip'] = $this->request->ip();
                $result = $this->page->createNewFeedBack($postData);

                // 发送邮箱
                $landpage_data = [
                    "title" => "官网询盘留言",
                    "username" => $this->request->post('user_name', ''),
                    "email" => $this->request->post('user_email', ''),
                    "phone" =>  $this->request->post('user_tel', ''),
                    "message" => $this->request->post('msg_content', ''),
                    "url" => $this->request->post('url', ''),
                    "custom_mail" => $this->systemInfo['email'],
                    "custom_company" => $this->systemInfo['name'],
                    'ip_addr'=>$this->request->ip()
                ];

                $this->sendMail($landpage_data);


                if ($result['state']) {
                    return $this->respMsg(\config('code.SUCCESS'), \lang('SUCCESS'));

                } else {
                    return $this->respMsg(\config('code.FAILURE'), \lang('FAILURE'));
                }
            }
        } catch (\Exception $e) {
            return $this->respMsg(\config('code.FAILURE'), $e->getMessage() . " " . $e->getFile() . $e->getLine());
        }
    }

    private function validateMobile($mobile)
    {
        $pattern = '/^1[3-9]\d{9}$/';
        if (preg_match($pattern, $mobile)) {
            return true;
        } else {
            return false;
        }
    }

    /*public function cases()
    {
        $params = $this->request->get();
        $params['page_type'] = 3;
        $parentId = $params['id'] ?? 0;
        //$params['parent_id'] = ['<>', $parentId];
        $result = $this->page->getPageListByPaginate($params);
        $cases = $result['list'];
        for ($i = 0; $i < count($cases); $i++) {
            $cases[$i]['detailUrl'] = url('casesDetail', ['id' => $cases[$i]['page_id']]);
        }
        View::assign('items', $cases);
        View::assign('pageInfo', $result['pageInfo']);
        // 加载右侧悬浮
        $this->getServicesRightMenus('cases');


        $this->page_id = 2;
        $adData = $this->page->getAdData("*", [['page_id', '=', $this->page_id]]);
        View::assign('adData', $adData);

        if ($parentId > 0) {
            $catName = $this->page->getPageDetail($parentId, [])['data']['navName'];
            View::assign('catName', $catName);
        }

        return $this->responseFetchHtml('cases');
    }*/

    public function casesDetail($id)
    {
        $item = $this->page->getPageDetail($id, [])['data'];
        $nearItems = $this->page->getPageForPreAndNext($id, ['page_type' => 3]);
        View::assign('item', $item);
        View::assign('nearItems', $nearItems);

        // 加载右侧悬浮
        $this->getServicesRightMenus('cases');

        $navData = $this->page->getNavigateId('cases');
        $pageData = $this->page->getPageData($navData['id']);
        $adData = $this->page->getAdData("*", [['page_id', '=', $pageData['page_id']]]);
        View::assign('adData', $adData);

        $parentId = $item['parent_id'];
        if ($parentId > 0) {
            $catName = $this->page->getPageDetail($parentId, [])['data']['navName'];
            View::assign('catName', $catName);
        }

        return $this->responseFetchHtml();
    }


    /**
     * 下载中心
     * @return string|null
     */
    private function doDownload()
    {
        $downloadList = $this->page->getDownloadList(['all' => 1]);
        $this->dataList = $downloadList;
        $this->redirectName = 'download';
    }

    /**
     * 服务中心右边菜单
     * @return void
     */
    public function getServicesRightMenus($action = 'services'): void
    {
        $currentMenus = [];
        for ($i = 0; $i < count($this->menus); $i++) {
            if ($action == $this->menus[$i]['en_name']) {
                $currentMenus = $this->menus[$i];
                break;
            }
        }
        View::assign('currentMenus', $currentMenus);
    }

    /**
     * 处理partner模块
     * @return void
     */
    private function doPartner()
    {
        $catId = $this->request->param('id', 0);
        $condition = [];
        if ($catId == 0) {
            $condition['nav_id'] = $this->navData['id'];
        }
        $this->pageSize = 12;
        $cmsList = $this->page->getArticleListByPages($this->pageIndex, $this->pageSize, $catId, $condition);
        $this->pageInfo = $cmsList['pageInfo'];
        $this->dataList = $cmsList['list'];
        $catInfo = [];
        if ($catId > 0) {
            $cmsCat = $this->page->getCmsCatDetail($catId);
            $catInfo = $cmsCat['data'];
        }
        $catInfo['cat_name'] = $catInfo['cat_name'] ?? '全部';
        View::assign('catInfo', $catInfo);

        View::assign('cmsCatList', $this->navData['cmsCatList']);
        $this->catId = $catId;
    }

    /**
     * 处理partner info 页面
     * @return void
     */
    private function doPartnerInfo()
    {
        $this->getBlogs(3);
        $navItem = $this->page->getNavigateId('partner');
        $pageData = $this->page->getPageData($navItem['id']);
        $pagePartList = $this->page->getPagePartList($pageData['page_id']);
        View::assign('pagePartList', $pagePartList);


        $cmsCatList = $this->page->getCmsCatList(['nav_id' => $navItem['id'], 'all' => 1]);
        View::assign('cmsCatList', $cmsCatList);
        $detail = $this->page->getCmsDetail($this->request->param('id'));
        $this->dataItem = $detail['data'];
        $this->catId = $this->dataItem['cat_id'];
        View::assign('catInfo', $this->dataItem['cmsCat']);
    }

    /**
     * 获取blog的新闻
     * @param $count
     * @return void
     */
    private function getBlogs($count = 10, $search = null)
    {
        // 读取最新三条新闻
        $navItem = $this->page->getNavigateId('blog');
        $news = $this->page->getArticleListByPages(
            $this->pageIndex,
            $count,
            $this->catId,
            ['is_show' => 1],
            $search
        );
        View::assign('news', $news['list']);
        View::assign('search', $search);
        $this->pageInfo = $news['pageInfo'];
        $this->dataList = $news['list'];
    }

    /**
     * blog分类
     * @param $action
     * @return void
     */
    private function getBlogsCat($action = 'blog')
    {
        $navItem = $this->page->getNavigateId('blog');
        $catList = $this->page->getCmsCatList(['all' => 1, 'nav_id' => $navItem['id'], 'cat_ids' => $this->articeType['news']]);
        $this->catList = $catList;
    }

    /**
     * blog分类
     * @param $action
     * @return void
     */
    private function getAdvantageCat($action = 'blog')
    {
        $navItem = $this->page->getNavigateId('advantage');
        $catList = $this->page->getCmsCatList(['all' => 1, 'nav_id' => $navItem['id'], 'cat_ids' => $this->articeType['advantage']]);
        $this->catList = $catList;
    }

    /**
     * 获取证书的方法
     * @param $count
     * @return void
     */
    private function getCertifications($count = 10)
    {
        if ($count == null) {
            $params = ['all' => 1];
        } else {
            $params = [
                'page' => $this->pageIndex,
                'limit' => $count
            ];
        }
        $certifications = $this->certificationsLogic->getCertificationList($params);
        $this->dataList = $certifications;
        View::assign('certifications', $certifications);

    }

    /**
     * 获取合作伙伴的方法
     * @param $count
     * @return void
     */
    private function getPartners($count = 10)
    {
        if ($count == null) {
            $params = ['all' => 1];
        } else {
            $params = [
                'page' => $this->pageIndex,
                'limit' => $count
            ];
        }
        $partners = $this->partnerLogic->getPartnerList($params);
        $this->dataList = $partners['list'];
        $this->pageInfo = $partners['pageInfo'];
        View::assign('partners', $partners);
    }

    /**
     * 处理证书方法
     * @return void
     */
    private function doCertifications()
    {
        if ($this->request->param('id') > 0) {
            $this->redirectName = 'certificationsinfo';
            $this->doCertificationsinfo();
        }
        $this->getCertifications(null);
    }

    /**
     * 服务品牌/合作伙伴
     * @return void
     */
    private function doServices()
    {
        $this->getPartners(9);
    }

    /**
     * 资质详情
     * @return void
     */
    private function doCertificationsinfo()
    {
        $id = $this->request->param('id');
        $this->dataItem = $this->certificationsLogic->getCertificationDetail($id);
    }

    /**
     * 加入我们页面
     * @return void
     */
    private function doJoinUs()
    {
        // 岗位列表
        if ($this->request->param('id')) {
            $id = $this->request->param('id');
            $this->dataItem = $this->recruitmentLogic->getRecruitmentDetail($id);
            $this->redirectName = 'joinusinfo';
        } else {
            $params['page'] = $this->request->param('page');
            $params['limit'] = $this->request->param('limit');
            $params['desc'] = $this->request->param('desc', '');
            $params['address'] = $this->request->param('address', '');
            $params['keywords'] = $this->request->param('keywords', '');
            $params['type_name'] = $this->request->param('type_name', '');
            $params['all'] = 1;
            $this->dataList = $this->recruitmentLogic->getRecruitmentListType($params);
            View::assign('joinUsParams', $params);
            $this->redirectName = 'joinus';
        }
    }

    /**
     * 技术服务
     * @return void
     */
    private function doService()
    {
        $this->needForms = true;
        $downloadList = $this->page->getDownloadList(['is_top' => 1, 'limit' => 3]);
        View::assign('downloads', $downloadList['list']);
        $this->redirectName = 'service';
    }

    private function doFaq()
    {
        $this->dataList = $this->page->getQuestionList(['all' => 1]);
        $this->redirectName = 'faq';
    }

    private function doAbout()
    {
        $this->getCertifications(null);
        $this->redirectName = 'about';
    }

    /**
     * 获取分类详情
     * @return void
     */
    private function getBlogsCatDetail($catId)
    {
        $catInfo = $this->page->getCmsCatDetail($catId);
        View::assign('catInfo2', $catInfo['data']);
        View::assign('catInfo', $catInfo['data']);
    }

    /**
     * 发送邮件
     * @param $data
     * @return void
     */
    private function sendMail($data)
    {
        $mail = new PHPMailer(true);
        $config = $this->page->getConfigDetailByCode('email')['data'];
        $content = '<div style="width: 900px; margin: 0 auto;">
        <ul style=" width:100%; margin: 40px auto 30px; list-style: none; border-radius: 20px; padding:25px 22px; background: #f8fbfe;
border-radius: 10px;">
            <li style="display: flex; align-items: center; padding: 10px 0; font-size: 16px; height: 50px;  border-radius: 4px; ">
                <span style="font-size: 16px; font-weight: bold; color: #111; width:120px;  margin-right: 30px; padding:0 15px;">
                    用户名称: </span>
                <span style="color: #555;"> ' . $data['username'] . '</span>
            </li>
            <li style="display: flex; align-items: center; padding: 10px 0; font-size: 16px; height: 42px;  background: #ffffff; border-radius: 4px; ">
                <span style="font-size: 16px; font-weight: bold; color: #111; width:120px;  margin-right: 30px; padding:0 15px;">
                    手机号: </span>
                <span style="color: blue; text-decoration: underline;"> ' . $data['phone'] . ' </span>
            </li>
            <li style="font-size: 16px; ">
                <div style="font-size: 16px; font-weight: bold; color: #111; width:120px;  padding:0 15px; height: 50px; line-height: 50px;">
                    留言内容:
                </div>
                <p style="color: #555; width: 100%; min-height:115px ; background: #ffffff; padding: 15px;">
                    ' . $data['message'] . '
                </p>
            </li>
        </ul>
        <div style="margin: 30px 0; width: 100%; padding:25px 22px; background-color: #f8fbfe;  border-radius: 10px;">
            <li style="display: flex; align-items: center; padding: 10px 0; font-size: 16px; height: 50px;  background: #ffffff;  border-radius: 4px;">
                <span style="font-size: 16px; font-weight: bold; color: #111; width:120px;  margin-right: 30px; padding:0 15px;">
                    IP地址：</span>
                <span style="color: #555;">' . $data['ip_addr'] . '</span>
            </li>
            <li style="font-size: 16px;display: flex; align-items: flex-start; ">
                <div style="font-size: 16px; font-weight: bold; color: #111; width:120px;  padding:0 15px; height: 50px; line-height: 50px;">
                    推广页面:
                </div>
                <p style="color: #555; width: 100%;  padding: 15px;">
                    ' . urldecode($data['url']) . '
                </p>
            </li>
        </div>
        <div style="margin: 30px 0; width: 100%; padding:25px 22px; background-color: #f8fbfe;  border-radius: 10px;">
            <div style="display: flex ; align-items: flex-start;">
                <div style="display: flex ; align-items: center; justify-content: center; width: 20px; height: 20px; background-color: #3ba160; border-radius: 50%; color: #ffffff;font-size: 18px;">!</div>
                <div style="font-size: 16px;  margin-left:10px; color: #555;"> 
                    为了正常接收到我们邮件，请将我们的发信地址添加到白名单。如果未找到邮件信息，请在垃圾箱中查找<br>
                    如果您不想在接收到此邮件，请回复此邮件内容为退订，后面我们将不再向您发送此邮件。
                    
                </div>
                
            </div>
           
        </div>
    </div>';
        try {
            //服务器设置
            $mail->isSMTP();                                            // 使用 SMTP
            $mail->Host = 'smtp.qq.com';                       // 设置邮件服务器
            $mail->SMTPAuth = true;                                     // 启用 SMTP 验证
            $mail->Username = '1017503523@qq.com';                 // SMTP 用户名
            $mail->Password = 'qtgbqkcqobgvbfag';                    // SMTP 密码
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;           // 启用 TLS 加密
            $mail->Port = 587;                                      // TCP 端口

            // 收发件人设置
            $mail->setFrom('1017503523@qq.com', '华仁环保官网');
            $mail->addAddress($config['value'], $config['value']);      // 添加收件人
            $mail->addReplyTo('1604760673@qq.com', '抄送人');  // 回复地址

            // 内容设置
            $mail->isHTML(true);                                          // 设置邮件内容为 HTML
            $mail->Subject = $data['title'];
            $mail->Body = $content;
            $mail->AltBody = $data['title'];

            // 发送邮件
            $result = $mail->send();
            $raw = ['time' => date('Y-m-d H:i:s'), 'data' => $data];
            logDebug($raw);
            if ($result) {
                logDebug('邮件发送成功');
            } else {
                logDebug($mail->ErrorInfo);
            }
        } catch (\Exception $e) {
            logError($e);
        }
    }

}
